package com.icow.wms.basic.controller;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import com.icow.wms.basic.domain.Vehicle;
import com.icow.wms.basic.domain.VehicleAllowedOrderTypesTO;
import com.icow.wms.basic.utils.OpenTcs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.icow.common.annotation.Log;
import com.icow.common.core.controller.BaseController;
import com.icow.common.core.domain.AjaxResult;
import com.icow.common.enums.BusinessType;
import com.icow.wms.basic.domain.AgvType;
import com.icow.wms.basic.service.IAgvTypeService;
import com.icow.common.utils.poi.ExcelUtil;
import com.icow.common.core.page.TableDataInfo;

import javax.annotation.PostConstruct;

/**
 * agvTypeController
 *
 * @author bcs
 * @date 2024-08-21
 */
@Slf4j
@RestController
@RequestMapping("/wcs/agvType")
public class AgvTypeController extends BaseController {
    @Autowired
    private IAgvTypeService agvTypeService;


    @PostConstruct
    public void agvTypeInit() {
        List<Vehicle> vehicles = OpenTcs.getVehicles();
        List<AgvType> agvTypes = agvTypeService.selectAgvTypeList(new AgvType());
        //删除多余的agvType
        for (AgvType agvType : agvTypes) {
            AtomicBoolean isNull = new AtomicBoolean(true);
            vehicles.forEach(vehicle -> {
                if (agvType.getAgvName().equals(vehicle.getName())) {
                    isNull.set(false);
                }
            });
            if (isNull.get()) {
                agvTypeService.deleteAgvTypeById(agvType.getId());
            }
        }
        //添加没有添加的agvType
        List<AgvType> finalAgvTypes = agvTypeService.selectAgvTypeList(new AgvType());
        vehicles.forEach(vehicle -> {
            AtomicBoolean isNull = new AtomicBoolean(true);
            for (AgvType agvType : finalAgvTypes) {
                if (vehicle.getName().equals(agvType.getAgvName())) {
                    isNull.set(false);
                    break;
                }
            }
            if (isNull.get()) {
                AgvType agvType = new AgvType();
                agvType.setAgvName(vehicle.getName());
                agvType.setAgvType(vehicle.getAllowedOrderTypes().toString().replace("[", "").replace("]", ""));
                agvTypeService.insertAgvType(agvType);
            }
        });
        agvTypes = agvTypeService.selectAgvTypeList(new AgvType());
        for (AgvType agvType : agvTypes) {
            List<String> temps = Arrays.asList(agvType.getAgvType().split("\\,"));
            OpenTcs.markVehicleType(agvType.getAgvName(), new VehicleAllowedOrderTypesTO(temps));
        }
        log.info("agvType init success");
    }

    /**
     * 查询agvType列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AgvType agvType) {
        startPage();
        List<AgvType> list = agvTypeService.selectAgvTypeList(agvType);
        return getDataTable(list);
    }

    /**
     * 导出agvType列表
     */
    @Log(title = "agvType", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AgvType agvType) {
        List<AgvType> list = agvTypeService.selectAgvTypeList(agvType);
        ExcelUtil<AgvType> util = new ExcelUtil<AgvType>(AgvType.class);
        return util.exportExcel(list, "agvType");
    }

    /**
     * 获取agvType详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agvTypeService.selectAgvTypeById(id));
    }

    /**
     * 新增agvType
     */
    @Log(title = "agvType", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgvType agvType) {
        if (agvType == null || agvType.getAgvName() == null || agvType.getAgvType() == null) {
            return toAjax(-1, "agvName和agvType不能为空");
        }
        List<String> temps = Arrays.asList(agvType.getAgvType().split("\\,"));
        boolean markVehicleType = OpenTcs.markVehicleType(agvType.getAgvName(), new VehicleAllowedOrderTypesTO(temps));
        if (!markVehicleType) {
            return toAjax(-1, agvType.getAgvName() + " 车辆名称错误,请检查");
        }
        return toAjax(agvTypeService.insertAgvType(agvType));
    }

    /**
     * 修改agvType
     */
    @Log(title = "agvType", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgvType agvType) {
        if (agvType == null || agvType.getAgvName() == null || agvType.getAgvType() == null) {
            return toAjax(-1, "agvName和agvType不能为空");
        }
        List<String> temps = Arrays.asList(agvType.getAgvType().split("\\,"));
        boolean markVehicleType = OpenTcs.markVehicleType(agvType.getAgvName(), new VehicleAllowedOrderTypesTO(temps));
        if (!markVehicleType) {
            return toAjax(-1, agvType.getAgvName() + " 车辆名称错误,请检查");
        }
        return toAjax(agvTypeService.updateAgvType(agvType));
    }

    /**
     * 删除agvType
     */
    @Log(title = "agvType", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            AgvType agvType = agvTypeService.selectAgvTypeById(id);
            if (agvType == null || agvType.getAgvName() == null) {
                continue;
            }
            OpenTcs.markVehicleType(agvType.getAgvName(), new VehicleAllowedOrderTypesTO(Arrays.asList("*")));
        }
        return toAjax(agvTypeService.deleteAgvTypeByIds(ids));
    }
}